package com.web.cms.pan.provider;

import com.blue.core.dict.State;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.DeleteModel;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.blue.validation.spi.IdListValidator;
import com.web.cms.pan.cache.FolderCache;
import com.web.cms.pan.exception.FolderException;
import com.web.cms.pan.facade.FolderFacade;
import com.web.cms.pan.message.FolderRequest;
import com.web.cms.pan.message.FolderResponse;
import com.web.cms.pan.model.Folder;
import com.web.cms.pan.service.FolderService;
import com.web.cms.pan.util.FolderConvertor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 目录接口实现类
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2018年04月09日
 */
@Component("panFolderProvider")
public class FolderProvider implements FolderFacade
{
	@Autowired
	private FolderService folderService;
	@Autowired
	private FolderCache folderCache;
	
	public FolderProvider()
	{
	}

	@Override
	public FolderResponse add(FolderRequest request)
	{
		ValidationUtil.valid(request, SaveModel.class);
		Folder folder = FolderConvertor.toFolder(request);
		folder = folderService.save(folder);
		folderCache.delete(folder.getUserId());
		FolderResponse response = FolderConvertor.toResponse(folder);
		return response;
	}

	@Override
	public FolderResponse update(FolderRequest request)
	{
		ValidationUtil.valid(request, UpdateModel.class);
		Folder folder = FolderConvertor.toFolder(request);
		folder = folderService.save(folder);
		folderCache.delete(folder.getUserId());
		FolderResponse response = FolderConvertor.toResponse(folder);
		return response;
	}

	@Override
	public void state(FolderRequest request)
	{
		ValidationUtil.valid(request, DeleteModel.class);
		Folder folder = FolderConvertor.toFolder(request);
		folderService.state(folder);
		folderCache.delete(folder.getUserId());
	}

	@Override
	public FolderResponse get(Integer id)
	{
		if (id == null)
			throw new FolderException("ID不能为空");
			
		Folder folder = folderService.get(id);
		FolderResponse response = FolderConvertor.toResponse(folder);
		return response;
	}

	@Override
	public List<FolderResponse> list(Integer userId, Integer parentId, State state)
	{
		if (userId == null)
			throw new FolderException("用户ID不能为空");

		Folder folder = new Folder();
		folder.setUserId(userId);
		folder.setParentId(parentId);
		folder.setState(state);
		List<Folder> list = folderService.list(folder);
		List<FolderResponse> responseList = FolderConvertor.toResponse(list);
		return responseList;
	}

	@Override
	public List<FolderResponse> listPath(Integer userId, Integer id)
	{
		if (userId == null)
			throw new FolderException("用户ID不能为空");
		if (id == null)
			throw new FolderException("目录ID不能为空");

		List<Folder> list = folderCache.listPath(userId, id);
		List<FolderResponse> responseList = FolderConvertor.toResponse(list);
		return responseList;
	}

	@Override
	public Map<String, List<FolderResponse>> listTree(Integer userId)
	{
		if (userId == null)
			throw new FolderException("用户ID不能为空");

		Map<Integer, List<Folder>> map = folderCache.listTree(userId);
		List<Folder> list = map.get(0);
		if (list == null)
		{
			list = new ArrayList<>();
			map.put(0, list);
		}
		Folder root = new Folder();
		root.setId(-1);
		root.setParentId(0);
		root.setName("根目录");
		root.setFileSize(0L);
		list.add(0, root);
		Map<String, List<FolderResponse>> responseMap = FolderConvertor.toTreeMap(map);
		return responseMap;
	}

	@Override
	public void operate(Integer userId, int srcId, int destId, int type)
	{
		if (userId == null)
			throw new FolderException("用户ID不能为空");
		if (type == 0)
			throw new FolderException("未知操作");

		if (destId == -1)
			destId = 0;

		if (!folderCache.valid(userId, srcId, destId))
			throw new FolderException("用户ID和目录ID不匹配");

		if (type == 1) // 移动
		{
			if (folderCache.inTree(userId, destId, srcId))
				throw new FolderException("源目录不能移动到目标目录中");

			folderService.move(srcId, destId);
		}
		else if (type == 2) // 复制
		{
			folderService.copy(userId, srcId, destId);
		}
		folderCache.delete(userId);
	}

	@Override
	public void delete(Integer userId, String ids)
	{
		if (userId == null)
			throw new FolderException("用户ID不能为空");
		if (ids == null || ids.isEmpty() || !IdListValidator.isValid(ids))
			throw new FolderException("目录ID列表错误");

		List<Integer> idList = Arrays.stream(ids.split(",")).map(Integer::valueOf).collect(Collectors.toList());
		folderService.delete(userId, idList);
	}

}
